package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.DownloadsNewDownload;

public class GenericDownloadNewDownloadDAO extends AutoKeys implements
		org.jetnuke.dao.DownloadNewDownloadDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericDownloadNewDownloadDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadNewDownloadModel#retrieveByPK(int)
	 */
	public DownloadsNewDownload retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadNewDownloadModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		DownloadsNewDownload s = new DownloadsNewDownload();

		if (rs.next()) {
			fillDownNewDownFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	public void deleteByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadNewDownloadModel.deleteByID"));
		p.setInt(1, id);

		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPK(int) - end"); //$NON-NLS-1$
		}
	}

	/**
	 * @param s
	 * @param rs
	 */
	protected void fillDownNewDownFromResultSet(DownloadsNewDownload s,
			ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillDownNewDownFromResultSet(DownloadsNewDownload, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setCID(rs.getInt("CID"));
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setEMAIL(rs.getString("EMAIL"));
		s.setFILESIZE(rs.getInt("FILESIZE"));
		s.setHOMEPAGE(rs.getString("HOMEPAGE"));
		s.setLID(rs.getInt("LID"));
		s.setNAME(rs.getString("NAME"));
		s.setSID(rs.getInt("SID"));
		s.setSUBMITTER(rs.getString("SUBMITTER"));
		s.setTITLE(rs.getString("TITLE"));
		s.setURL(rs.getString("URL"));
		s.setVERSION(rs.getString("VERSION"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillDownNewDownFromResultSet(DownloadsNewDownload, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("DownloadNewDownloadModel.selectAll"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	private List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			DownloadsNewDownload u = new DownloadsNewDownload();
			fillDownNewDownFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadNewDownloadModel#countAll()
	 */
	public int countAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadNewDownloadModel.countAll"));

		ResultSet rs = p.executeQuery();
		rs.next();
		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - end"); //$NON-NLS-1$
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadNewDownloadModel#addNew(org.jetnuke.entities.DownloadsNewDownload)
	 */
	public int addNew(DownloadsNewDownload dd) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(DownloadsNewDownload) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("DownloadNewDownloadModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("DownloadNewDownloadModel.lastGeneratedDownloadNewID"));
		initNewDownload(dd, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(DownloadsNewDownload) - end"); //$NON-NLS-1$
		}
		return id;

	}

	/**
	 * @param dd
	 * @param p
	 */
	private void initNewDownload(DownloadsNewDownload dd, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewDownload(DownloadsNewDownload, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setInt(1, dd.getCID());
		p.setString(2, dd.getDESCRIPTION());
		p.setString(3, dd.getEMAIL());
		p.setInt(4, dd.getFILESIZE());
		p.setString(5, dd.getHOMEPAGE());
		p.setString(6, dd.getNAME());
		p.setInt(7, dd.getSID());
		p.setString(8, dd.getSUBMITTER());
		p.setString(9, dd.getTITLE());
		p.setString(10, dd.getURL());
		p.setString(11, dd.getVERSION());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewDownload(DownloadsNewDownload, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

}
